package com.blue.wechat.receive.parse;

import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.blue.core.dict.WechatRequestType;
import com.blue.wechat.receive.model.AbstractRequest;
import com.blue.wechat.receive.model.EventRequest;
import com.blue.wechat.receive.model.ImageRequest;
import com.blue.wechat.receive.model.LinkRequest;
import com.blue.wechat.receive.model.LocationRequest;
import com.blue.wechat.receive.model.TextRequest;
import com.blue.wechat.receive.model.VideoRequest;
import com.blue.wechat.receive.model.VoiceRequest;

public class XmlModelParser
{
	private XmlModelParser()
	{
	}

	public static AbstractRequest parse(String xml)
	{

		Document doc = Jsoup.parse(xml);
		Element root = doc.getElementsByTag("xml").first();

		return create(root);
	}

	private static AbstractRequest create(Element root)
	{
		String type = root.getElementsByTag("MsgType").text();
		WechatRequestType msgType = WechatRequestType.getType(type);

		AbstractRequest msg = null;
		if (msgType == WechatRequestType.TEXT)
		{
			msg = createText(root);
		}
		else if (msgType == WechatRequestType.LOCATION)
		{
			msg = createLocation(root);
		}
		else if (msgType == WechatRequestType.EVENT)
		{
			msg = createEvent(root);
		}
		else if (msgType == WechatRequestType.IMAGE)
		{
			msg = createImage(root);
		}
		else if (msgType == WechatRequestType.VOICE)
		{
			msg = createVoice(root);
		}
		else if (msgType == WechatRequestType.VIDEO || msgType == WechatRequestType.SHORT_VIDEO)
		{
			msg = createVideo(root);
		}
		else if (msgType == WechatRequestType.LINK)
		{
			msg = createLink(root);
		}

		if (msg != null)
		{
			msg.setType(msgType);
			msg.setToUserName(root.getElementsByTag("ToUserName").text());
			msg.setFromUserName(root.getElementsByTag("FromUserName").text());
			
			if (root.getElementsByTag("CreateTime").size() > 0)
			{
				long time = Long.parseLong(root.getElementsByTag("CreateTime").text());
				msg.setCreateTime(new Date(time * 1000));
			}
			else
			{
				msg.setCreateTime(new Date());
			}

			if (root.getElementsByTag("MsgId").size() > 0)
			{
				msg.setMsgId(Long.parseLong(root.getElementsByTag("MsgId").text()));
			}
		}

		return msg;
	}

	private static TextRequest createText(Element root)
	{
		TextRequest msg = new TextRequest();

		msg.setContent(root.getElementsByTag("Content").text());

		return msg;
	}

	private static LocationRequest createLocation(Element root)
	{
		LocationRequest msg = new LocationRequest();

		msg.setLocationX(Double.parseDouble(root.getElementsByTag("Location_X").text()));
		msg.setLocationY(Double.parseDouble(root.getElementsByTag("Location_Y").text()));
		msg.setScale(Double.parseDouble(root.getElementsByTag("Scale").text()));
		msg.setLabel(root.getElementsByTag("Label").text());

		return msg;
	}

	private static EventRequest createEvent(Element root)
	{
		EventRequest msg = new EventRequest();

		msg.setStrEvent(root.getElementsByTag("Event").text());
		msg.setEventKey(root.getElementsByTag("EventKey").text());
		msg.setStatus(root.getElementsByTag("Status").text());

		if (root.getElementsByTag("Ticket").size() > 0)
		{
			msg.setTicket(root.getElementsByTag("Ticket").text());
		}

		if (root.getElementsByTag("Latitude").size() > 0)
		{
			msg.setLatitude(Double.parseDouble(root.getElementsByTag("Latitude").text()));
		}
		if (root.getElementsByTag("Longitude").size() > 0)
		{
			msg.setLongitude(Double.parseDouble(root.getElementsByTag("Longitude").text()));
		}
		if (root.getElementsByTag("Precision").size() > 0)
		{
			msg.setPrecision(Double.parseDouble(root.getElementsByTag("Precision").text()));
		}

		return msg;
	}

	private static ImageRequest createImage(Element root)
	{
		ImageRequest msg = new ImageRequest();

		msg.setMediaId(root.getElementsByTag("MediaId").text());
		msg.setPicUrl(root.getElementsByTag("PicUrl").text());

		return msg;
	}

	private static VoiceRequest createVoice(Element root)
	{
		VoiceRequest msg = new VoiceRequest();

		msg.setMediaId(root.getElementsByTag("MediaId").text());
		msg.setFormat(root.getElementsByTag("Format").text());

		return msg;
	}

	private static VideoRequest createVideo(Element root)
	{
		VideoRequest msg = new VideoRequest();

		msg.setMediaId(root.getElementsByTag("MediaId").text());
		msg.setThumbMediaId(root.getElementsByTag("ThumbMediaId").text());

		return msg;
	}

	private static LinkRequest createLink(Element root)
	{
		LinkRequest msg = new LinkRequest();

		msg.setTitle(root.getElementsByTag("Title").text());
		msg.setDescription(root.getElementsByTag("Description").text());
		msg.setUrl(root.getElementsByTag("Url").text());

		return msg;
	}
}
